$(function () {
    // 登录
    $('#login').click(function(){
        let username=$('#login_username').val()
        let password=$('#login_password').val()
        $.ajax({
            type:'GET',
            url:'http://127.0.0.1:7300/mock/5ee7242cb1a45736b4181f4a/dog/userlist2?username='+username+'&password='+password,
            success:function(msg){
                let userlist=msg.data
                for(i in userlist){
                    if(username==userlist[i].username){
                        if(password==userlist[i].password){
                            $('#username').text(username).addClass('dropdown-toggle')
                            $('#zc_tc').text('退出')
                            alert(username+'登录成功')
                            $('#username').addClass('dropdown-toggle').attr('data-toggle','dropdown').attr('data-target','')
                        }else{
                            alert('密码错误')
                            break;
                        }
                        break;
                    }
                    else{
                        alert('找不到该用户')
                    }
                }
            },error:function(error){
                console.log(error.responseText);
                
            }
        })
    })
    // 传输数据到个人主页
    user_place()
    let p_head=$('#p_head')
    let p_name=$('#p_name')
    let p_type=$('#p_type')
    function user_place(){
        $.ajax({
            type:'GET',
            url:'http://localhost:7300/mock/5ee7242cb1a45736b4181f4a/dog/user-place?name=鱼蛋',
            success:function(msg){
                p_name.val(msg.data.name)
                p_head.attr('src',msg.data.head)
                p_type.val(msg.data.type)
            },
            error:function(error){
                console.log(error.responseText);
            }
        })
    }
    $('#p_modify').click(function(){
        $.ajax({
            type:'GET',
            url:'http://localhost:7300/mock/5ee7242cb1a45736b4181f4a/dog/modify?name='+p_name.val()+"&type="+p_type.val(),
            success:function(msg){
               alert(msg.data.result)
            },
            error:function(error){
                console.log(error.responseText);
            }
        })
    })
    //退出
    $('#zc_tc').click(function(){
        if($('#zc_tc').text()=='退出'){
            // alert('退出成功')
            // $('#zc_tc').text("注册")
            // $('#username').text('登录').removeClass('dropdown-toggle').attr('data-toggle','modal').attr('data-target','#login_modal')
            window.location.reload()
        }
    })
    // 随机生成帖子
    zhexiantu()
    for (let i = 1; i <= 8; i++) {
        Mock.mock('http://localhost:7300/mock/5ee7242cb1a45736b4181f4a/dog/news', {
            "tie": [{
                "text": Mock.Random.cparagraph(),
                "date": Mock.Random.date()
            }]
        })
        $.get('http://localhost:7300/mock/5ee7242cb1a45736b4181f4a/dog/news', function (data) {
            // console.log(JSON.parse(data).tie[0]);
            let tie_content = JSON.parse(data).tie[0]
            let tie = tie_content.text
            let time = tie_content.date
            $('#tieZi li:nth-child('+i+') a').text(tie)
            $('#tieZi li:nth-child('+i+') span').text(time)
        })
    }
    //随机生成网站数据统计
    {
    for(i of $("#hy_shuju tr")){
      test(i)
    }
    function test(i){
        Mock.mock('http://shuju', {
            "shuju": [{
                "today": Mock.Random.integer(10,500),
                "yesterday":Mock.Random.integer(10,500)
            }]
        })
        $.get('http://shuju', function (data) {
            let sj_back = JSON.parse(data).shuju[0]
               zz(sj_back)
            //    console.log(i);
        })
        function zz(a){
            // console.log( $(i).children().text());
            $(i).children("td:nth-child(2)").text(a.today)
            $(i).children("td:nth-child(3)").text(a.yesterday)
        }
    }
}


    $.ajax({
        type: "GET",//发送请求的类型 post get
        url: " http://localhost:7300/mock/5ee7242cb1a45736b4181f4a/dog/news",//发送的地址
        // data: "name=xx&location=Boston",//传递的参数 如果式get则可以省略这一条
        success: function (msg) {//成功的回调
            // alert("data saved"+msg);
            // console.log(msg.data);
            // let data = JSON.parse(msg.data);
            // console.log(data);
        },
        error: function (error) {
            console.log(error.responseText);
        }

    });

    // 折线图
    function zhexiantu(){
        Mock.mock('http://human', {
            'list':[{
                'man':[{
                    'Monday':Mock.Random.integer(50,500),
                    'Tuesday':Mock.Random.integer(50,500),
                    'Wednesday':Mock.Random.integer(50,500),
                    'Thursday':Mock.Random.integer(50,500),
                    'Friday':Mock.Random.integer(50,500),
                    'Saturday':Mock.Random.integer(50,500),
                    'Sunday':Mock.Random.integer(50,500)
                }],
                'woman':[{
                    'Monday':Mock.Random.integer(50,500),
                    'Tuesday':Mock.Random.integer(50,500),
                    'Wednesday':Mock.Random.integer(50,500),
                    'Thursday':Mock.Random.integer(50,500),
                    'Friday':Mock.Random.integer(50,500),
                    'Saturday':Mock.Random.integer(50,500),
                    'Sunday':Mock.Random.integer(50,500)
                }],
                'unknow':[{
                    'Monday':Mock.Random.integer(50,500),
                    'Tuesday':Mock.Random.integer(50,500),
                    'Wednesday':Mock.Random.integer(50,500),
                    'Thursday':Mock.Random.integer(50,500),
                    'Friday':Mock.Random.integer(50,500),
                    'Saturday':Mock.Random.integer(50,500),
                    'Sunday':Mock.Random.integer(50,500)
                }]
               
            }]
        })
        $.get('http://human', function (data) {
            let man_sj = JSON.parse(data).list[0].man[0]
            let woman_sj =JSON.parse(data).list[0].woman[0]
            let unknow_sj = JSON.parse(data).list[0].unknow[0]
            //    console.log(man_sj);
                //初始化ehcharts实例
    var myChart=echarts.init(document.getElementById("week_man"));
    //指定图表的配置项和数据
    var option={
        //图例-每一条数据的名字叫销量
        legend:{
            data:['男会员','女会员','非会员']
        },
        //x轴
        xAxis:{
            data:["周一","周二","周三","周四","周五","周六","周日"]
        },
        //y轴没有显式设置，根据值自动生成y轴
        yAxis:{},
        //数据-data是最终要显示的数据
        series:[{
            smooth:true,
            name:'男会员',
            type:'line',
            data:[man_sj.Monday,man_sj.Tuesday,man_sj.Wednesday,man_sj.Thursday,man_sj.Friday,man_sj.Saturday,man_sj.Sunday],
            // areaStyle: {
            //     normal: {}
            // },
            color:'#7db0d0'
        },
        {   
            smooth:true,
            name:'女会员',
            type:'line',
            data:[woman_sj.Monday,woman_sj.Tuesday,woman_sj.Wednesday,woman_sj.Thursday,woman_sj.Friday,woman_sj.Saturday,woman_sj.Sunday],
            // areaStyle: {
            //     normal: {}
            // },
            color:"#fc8484"
        },
        {   
            smooth:true,
            name:'非会员',
            type:'line',
            data:[unknow_sj.Monday,unknow_sj.Tuesday,unknow_sj.Wednesday,unknow_sj.Thursday,unknow_sj.Friday,unknow_sj.Saturday,unknow_sj.Sunday],
            // areaStyle: {
            //     normal: {}
            // },
            color:"#ebe6b8"
        }

    ]
    };
    //使用刚刚指定的配置项和数据项显示图表
    myChart.setOption(option);
        })
        
    }
    // 留言
    $("#liuyan").click(function(){
        let liuyan_text = $('#liuyan_text').val()
        $.ajax({
          type:"POST",
          url:"http://localhost:7300/mock/5ee7242cb1a45736b4181f4a/dog/say",
          data:"name=鱼蛋&text="+liuyan_text,
          success :function(msg){
            // let msg_r = JSON.parse(msg)
            xs(msg.data)
          },
          error:function(error){
            console.log(error.responseText);
          }
          });
        
        function xs(msg){
            // console.log(msg)
            for(i of $('#liuyan_ul li')){
                // console.log($(i).children('a'));
                $(i).children('a').text(msg)
            }
            for(let i =1 ; i<=3;i++){
                $('#liuyan_ul li:nth-child('+i+')').children('a').text(msg[i-1].text)
                $('#liuyan_ul li:nth-child('+i+')').children('p').text(msg[i-1].name)
                $('#liuyan_ul li:nth-child('+i+')').children('span').text(msg[i-1].time)
            }
        }
        $('#liuyan_text').val("")
        
    })
    // 服务器状态

  
    function fwqq(){   
        let num=0;
            Mock.mock('http://nums', {
                "f_num": [{
                    "cpu": Mock.Random.integer(1,99),
                    "gpu": Mock.Random.integer(1,99),
                    "ram": Mock.Random.integer(1,99),
                    "dis": Mock.Random.integer(1,99)
                }]
            })
            $.get('http://nums', function (data) {
                let cpu_num = JSON.parse(data).f_num[0]
                num_cpu=cpu_num.cpu
                num_gpu=cpu_num.gpu
                num_ram=cpu_num.ram
                num_dis=cpu_num.dis
                antime()
            })
        
        let num2=0;
        let timeID=null;
    function antime(){
        timeID=setInterval(()=>{
            fwq();
        },8)
    }
    function fwq(){
        if(num2<=99){
            $('#cpu').children('span').text(num2+"%");
            $('#gpu').children('span').text(num2+"%");
            $('#ram').children('span').text(num2+"%");
            $('#dis').children('span').text(num2+"%");
            console.log();
            num2++
        }else{
            $('#cpu').children('span').text(num_cpu+"%");
            $('#gpu').children('span').text(num_gpu+"%");
            $('#ram').children('span').text(num_ram+"%");
            $('#dis').children('span').text(num_dis+"%");
            clearInterval(timeID)
        }
    }
    }
    let take = 1
    $(window).scroll(function () {
        if ($(window).scrollTop() > 551&& take == 1) {
            fwqq()
            take=0
        }
        })

        function alert(msg){
            let  mo=`
            <div class="modal fade show modal_alert"  style="display: block; padding-right: 17px;" aria-modal="true" >
                                    <div class="modal-dialog modal-lg" style="">
                                        <div class="modal-content" style="">
                                            <!-- 模态框头部 -->
                                            <div class="modal-header">
                                                <h4 class="modal-title">提示</h4>
                                                <button type="button" class="close" data-dismiss="modal">×</button>
                                            </div>
                                            <!-- 模态框主体 -->
                                            <div class="modal-body" style="">
                                               `+msg+`
                                                </div>
                                                <!-- 内容栏目结束 -->
                                                <!-- 模态框底部 -->
                                                <div class="modal-footer">
                                                    <button class="col-md-3 border bg-light alert_close" style="height: 50px;" data-dismiss="modal">确定</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>`
            $('body').append(mo)
    
            $('.alert_close').click(
                function(){
                    $('.modal_alert').removeClass('show').css('display','none')
                }
            )
        }
})